package com.gc.widget;

import java.io.File;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

	public final static String TAG = "widget";

	private WebView webview = null;
	private String cachePath = "/data/data/"+getClass().getPackage().getName()+"/files/";

	public final static String LAST_VIEWED = "lastviewed";
	

	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
        webview = new WebView(this);
        setContentView(webview);

		WebSettings settings = webview.getSettings();
		settings.setSavePassword(false);
		settings.setSaveFormData(false);
		settings.setJavaScriptEnabled(true);
		settings.setSupportZoom(false);
		settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		
		//webview.getZoomControls().setVisibility(View.GONE);
		
		webview.setWebViewClient(new WebViewClient() {

			@Override
			public void onLoadResource(WebView view, String url) {
				System.out.println("onLoadResource "+url);
				String path = url.substring(("file://"+cachePath).length());
				File f = new File(cachePath+path);
				//if(!f.exists()) {
					f.getParentFile().mkdirs();
					InputStream is = Widget.getWidget().getWidgetFile(path);
					if(is == null)
						System.out.println("file "+path+" not found");
					else
						Utils.write(is, f);
				//}
			}
			
		});
		//webview.setWebChromeClient(new OilCanChrome());
		
		Widget.getWidget().loadFeatures(webview,this);

		String path = "index.html";
		webview.loadUrl("file://"+cachePath+path);
	
	}
	
	protected void onSaveInstanceState(Bundle outState) {
		outState.putString(LAST_VIEWED, webview.getUrl());
	}

	public void onDestroy() {
		super.onDestroy();
		//this.scriptdb.close();
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		/*
		MenuItem gourl = menu.add(R.string.browse_gotourl);
		gourl.setIcon(R.drawable.ic_menu_goto);
		gourl.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				BrowserActivity.this.startSearch(webview.getUrl(), true, null, false);
				return true;
			}
		});
		
		MenuItem refresh = menu.add(R.string.browse_refresh);
		refresh.setIcon(R.drawable.ic_menu_refresh);
		refresh.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				webview.reload();
				return true;
			}
		});

		MenuItem scripts = menu.add(R.string.browse_manage);
		scripts.setIcon(android.R.drawable.ic_menu_agenda);
		scripts.setIntent(new Intent(BrowserActivity.this, ScriptListActivity.class));

		MenuItem example = menu.add(R.string.browse_example);
		example.setIcon(R.drawable.ic_menu_bookmark);
		example.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				final String[] examples = BrowserActivity.this.getResources().getStringArray(R.array.list_examples);
				new AlertDialog.Builder(BrowserActivity.this)
					.setTitle(R.string.browse_example_title)
					.setItems(examples, new OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							BrowserActivity.this.loadNewPage(examples[which]);
						}
					}).create().show();

				return true;
			}
		});
*/
		return true;
	}

	
	
}